/**
 * Created by zhangsong on 2019/9/23.
 */

import AlarmemailReducer from '@/dva/application/alarmemail';
import * as AlarmApi from '@/services/application/alarmemail';
import URI from '@/services/uri';
import moment from 'moment';
import { defaultPaginationParam } from '@/constants/styleConfig';

const urly = URI.application;

const { Namespace, State, Reducers, Effects } = AlarmemailReducer;

const Model = {
  namespace: Namespace,
  state: State,
  effects: {
    *[Effects.getAlarmemailList]({ data = {} }, { put, call }) {
      const {
        page = defaultPaginationParam.page,
        pageSize = defaultPaginationParam.pageSize,
        endTime,
        startTime,
        passNumber,
      } = data;
      yield put({
        type: Reducers.setState,
        data: {
          listLoading: true,
        },
      });
      const res = yield call(AlarmApi.getAlarmList, data);
      console.log('进来的时候就会有', data)
      console.log('警報人員的信息 ', res);
      if (res.code === 200) {
        yield put({
          type: Reducers.setState,
          data: {
            listLoading: false,
            listData: res.data,
            page: res.page,
            pageSize: res.page_size,
            total: res.total,
            endTime,
            startTime,
            passNumber,
          },
        });
      } else {
        yield put({
          type: Reducers.setState,
          data: {
            listLoading: false,
          },
        });
      }
    },
    *[Effects.modifyState]({ data = {} }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          listLoading: true,
        },
      });
      const res = yield call(AlarmApi.modifyState, data.data);
      console.log('關閉或者是取消這個警報 ', res);
      if (res) {
        yield put({
          type: Reducers.setState,
          data: {
            listLoading: false,
          },
        });
        yield put({
          type: Effects.getAlarmemailList,
          data: data.list,
        });
      } else {
        yield put({
          type: Reducers.setState,
          data: {
            listLoading: false,
          },
        });
      }
    },
    *[Effects.downLoadXlsx]({ data }, { put, call }) {

      yield put({
        type: Reducers.setState,
        data: {
          xslloading: true,
        },
      });
      const response = yield call(AlarmApi.alarmDownXlsx, data);
      console.log('下载的额东西', urly.alarm.downxlsx)
      // return;
      // // if (response) {
      // //   const url = window.URL.createObjectURL(new Blob([response]));
      // const link = document.createElement('a'); // 创建a标签
      // //   link.style.display = 'none';
      // link.href = urly.alarm.downxlsx; // 设置a标签路径
      // // link.download = `person—list${moment(data.startTime).format('YYYY-MM-DD')}-${moment(data.endTime).format('YYYY-MM-DD')}.xlsx`; // 设置文件名， 也可以这种写法 （link.setAttribute('download', '名单列表.xls');
      // document.body.appendChild(link);
      // link.click();
      // URL.revokeObjectURL(link.href); // 释放 URL对象
      // document.body.removeChild(link);
      const eleLink = document.createElement('a');
      eleLink.style.display = 'none';
      // eleLink.target = "_blank"
      eleLink.href = `${urly.alarm.downxlsx}?startTime=${data.startTime}&endTime=${data.endTime}`;
      // eleLink.href = record;
      document.body.appendChild(eleLink);
      eleLink.click();
      document.body.removeChild(eleLink);
      yield put({
        type: Reducers.setState,
        data: {
          xslloading: false,
        },
      });
    },
  },
  reducers: {
    [Reducers.setState](state, { data = {} } = {}) {
      return {
        ...state,
        ...data,
      };
    },
  },
};

export default Model;
